import { http } from "@/utils/http";
import { type BaseResponseType, baseUrlApi } from "@/api/types";
import { encryptByRSA } from "@/utils/auth";
import { getRSAKey } from "@/api/user";


/** 礼品信息 */
export interface GiftInfo {
    /** 礼品ID */
    goodsid: number;
    /** 礼品名称 */
    name: string;
    /** 礼品描述 */
    desc: string;
    /** 剩余数量 */
    inventory: number;
    /** 兑换积分 */
    point: number;
    /** 礼品图品 */
    photo: string;
    /** 礼品类型 */
    shipping?: {
        /** 类型id */
        shippingid: Number;
        /** 类型名称 */
        name: string;
    };
}

/** 礼品列表 */
export interface GiftsListResponse extends BaseResponseType {
    data: Array<GiftInfo>;
}


/** 获取所有礼品信息 */
export let getGiftsList = () => {
    return http.get<GiftsListResponse, any>(baseUrlApi("/product/gift"));
};

/** 创建礼品兑换订单 */
export let createGift = async (data) => {
    let resKey = await getRSAKey();
    // 加密数据
    let encryptData = encryptByRSA(resKey.publickey, data.pwd);
    data.pwd = encryptData;

    return http.post<any, any>(baseUrlApi("/order/gift/add"), { data });
};
